$(function(){
    function init(openId){
        var openId = localStorage.getItem("openId");
        $.ajax({
            type: "GET",
            url: baseUrl + "/rest/activeJoinController/list/"+openId,
            contentType:"application/json;charset=UTF-8",
            data: {
                pageNumber:'1',
                pageSize:'100',
                fxjOut04:openId
              },
            dataType: "json",
            success: function(data){
                console.log(openId)
                console.log(data)
                if(data.ok){
                    if(data.data.length){
                        let a = '';
                        $("#carDetail").html(' ');
                        for(let i=0;i<data.data.length;i++){
                            console.log(data.data[i].fxjOut13)
                            console.log(baseUrl)
                            a += '<div class="item">';
                            a += '<div class="item-a">';
                            a += '<div class="l">'+data.data[i].fxjOut06+'</div>';
                            a += '<div class="r">'+data.data[i].fxjOut11+'</div>';
                            a += '</div>';
                            a += '<div class="item-b">';
                            a += '<div class="l">';
                            a += '<img src="'+baseUrl+"/" + data.data[i].fxjOut13+'" alt="">';
                            a += '</div>';
                            a += '<div class="r">';
                            a += '<p class="a">'+data.data[i].fxjOut02+'</p>';
                            // a += '<p class="b">地址'+data.data[i].fxjOut15+'</p>';
                            a += '<p class="c">活动时间:'+data.data[i].fxjOut16+'</p>';
                            a +='<p class="button" data-name="'+data.data[i].fxjOut06+'"  data-id="'+data.data[i].id+'"  data-activeName="'+data.data[i].fxjOut02+'"  data-openId="'+data.data[i].fxjOut04+'"  data-tel="'+data.data[i].fxjOut07+'"  data-ifUsed="'+data.data[i].fxjOut11+'"  data-address="'+data.data[i].fxjOut15+'"  data-startTime="'+data.data[i].fxjOut16+'"  ><text style="color:#ffffff">立即使用</text></p>'
                            // a += '<p class="d">期数:12期</p>';
                            // a += '<p class="e">月供:3980元</p>';
                            // a += '<p class="a">'+data.data[i].carName+'</p>';
                            // a += '<p class="b">申请服务:'+data.data[i].carType+'</p>';
                            // a += '<p class="c">首租金额:'+data.data[i].carMoney+'元</p>';
                            // a += '<p class="d">期数:'+data.data[i].rentDate+'期</p>';
                            // a += '<p class="e">月供:'+data.data[i].rentMoney+'元</p>';
                            a += '</div>';
                            a += '</div>';
                            a += '</div>';
                        }
                        $("#order").html(a);
                        // 立即使用
                        $(".button").click(function(){
                            let name = $(this).attr('data-name');
                            let id = $(this).attr('data-id');
                            let activeName = $(this).attr('data-activeName');
                            let openId = $(this).attr('data-openId');
                            let tel = $(this).attr('data-tel');
                            let ifUsed = $(this).attr('data-ifUsed');
                            let address = $(this).attr('data-address');
                            let startTime = $(this).attr('data-startTime');
                            InfoData={
                                name:name,
                                id:id,
                                activeName:activeName,
                                openId:openId,
                                tel:tel,
                                ifUsed:ifUsed,
                                address:address,
                                startTime:startTime,

                            }
                            var url = 'http://www.zhaodui.com.cn/hdb/code/h5/view/qrCodeConfirm.html?id='+ id 
                            // var url = ""
                            // var url = 'a.html?openId=hbkjhnjnlk&goodid=1'
                            console.log(id)
                            console.log(openId)
                            console.log(url)
                            getQrcode(url);

                        });
                    }else{
                        $("#carDetail").html('暂无数据');
                    }
                }else{
                    alert(data.message);
                }
            }
        });
        
    }
    function tab(dataType,openId){
        $.ajax({
            type: "GET",
            url: baseUrl + "/rest/lkCarApplyController/list/"+openId+"?pageNumber=1&pageSize=100",
            contentType:"application/json;charset=UTF-8",
            data: {
                orderData: dataType,
            },
            dataType: "json",
            success: function(data){
                console.log(data)
                if(data.ok){
                    if(data.data.length){
                        let a = '';
                        $("#carDetail").html(' ');
                        for(let i=0;i<data.data.length;i++){
                            a += '<div class="item">';
                            a += '<div class="item-a">';
                            if(data.data[i].name){
                                a += '<div class="l">'+data.data[i].name+'</div>';
                            }else{
                                a += '<div class="l"></div>';
                            }
                            // a += '<div class="l">'+data.data[i].name+'</div>';
                            a += '<div class="r">'+data.data[i].orderData+'</div>';
                            a += '</div>';
                            a += '<div class="item-b">';
                            a += '<div class="l">';
                            a += '<img src="'+baseUrl+"/img/server/" + data.data[i].remake1+'" alt="">';
                            a += '</div>';
                            a += '<div class="r">';
                            a += '<p class="a">'+data.data[i].carName+'</p>';
                            a += '<p class="b">申请服务:'+data.data[i].carType+'</p>';
                            a += '<p class="c">首租金额:'+data.data[i].carMoney+'元</p>';
                            a += '<p class="d">期数:'+data.data[i].rentDate+'期</p>';
                            a += '<p class="e">月供:'+data.data[i].rentMoney+'元</p>';
                            a += '</div>';
                            a += '</div>';
                            a += '</div>';
                        }
                        $("#order").html(a);
                    }else{
                        $("#carDetail").html('暂无数据');
                    }
                }else{
                    alert(data.message);
                }
            }
        });
    }
    var openId = localStorage.getItem("openId");
    if(openId == null || openId == ''){
        messageBox("获取个人信息失败");
        setTimeout(function(){
            loginFail('1'); // 一键登录
        },200);
        return false;
    }
    init(openId);// 初始化加载数据
    // 切换加载数据
    $(".header-item").click(function(){
        $(this).addClass("active").siblings().removeClass("active");
        var dataType = $(this).attr("data-type");
        if(dataType == '0'){
            tab("未完成",openId);
        }
        if(dataType == '1'){
            tab("已完成",openId);
        }
        if(dataType == '2'){
            init(openId);
        }
    }); 

    
});

function getQrcode(url){
    // console.log(name)
    // console.log(id)
    // console.log(activeName)

    $(".qrcode-box").css("display","block");
    let _w = $("#qrcode").width();
    jQuery('#qrcode').qrcode({
        render: "canvas", //也可以替换为table
        width: _w,
        height: _w,
        text: url,
    });
}